package in.abip.chisame;

import in.abip.chisame.model.Room;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * @author Julot
 * @since Thursday, January 26th, 2011.
 */
public class RoomTabActivity extends TabActivity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_activity);
		changeTitle();
		bind();
		
		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost();  // The activity TabHost
		TabHost.TabSpec spec;  // Resusable TabSpec for each tab
		Intent intent;  // Reusable Intent for each tab
		
		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, LightListActivity.class);
		intent.putExtras(getIntent().getExtras());
		
		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("light");
		spec.setIndicator(
			res.getString(R.string.light),
			res.getDrawable(R.drawable.ic_tab_light)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		// Do the same for the other tabs
		intent = new Intent().setClass(this, HvacListActivity.class);
		intent.putExtras(getIntent().getExtras());
		spec = tabHost.newTabSpec("hvac");
		spec.setIndicator(
			res.getString(R.string.hvac),
			res.getDrawable(R.drawable.ic_tab_hvac)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, MoodListActivity.class);
		intent.putExtras(getIntent().getExtras());
		spec = tabHost.newTabSpec("mood");
		spec.setIndicator(
			res.getString(R.string.mood),
			res.getDrawable(R.drawable.ic_tab_mood)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, CameraListActivity.class);
		intent.putExtras(getIntent().getExtras());
		spec = tabHost.newTabSpec("camera");
		spec.setIndicator(
			res.getString(R.string.camera),
			res.getDrawable(R.drawable.ic_tab_camera)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		tabHost.setCurrentTab(0);
	}
	
	/** Change title */
	protected void changeTitle() {
		Bundle bundle = getIntent().getExtras();
		Integer roomId = bundle.getInt("roomId");
		Room room = (Room) Room.model().findByPk(roomId);
		TextView text = (TextView) findViewById(R.id.title);
		text.setText(room.name);
	}
	
	/** Bind button click. */
	protected void bind() {
		OnClickListener listener = new OnClickListener() {
			public void onClick(View view) {
				RoomTabActivity.this.finish();
			}
		};
		View view = (View) findViewById(R.id.home);
		view.setOnClickListener(listener);
	}
}
